// 导入数据库
let db = require("../db/index");
const router = require("../router/backstageRouter");

exports.test = (req, res) => {
    let sql = "SELECT * FROM `backstage_user`";
    db.query(sql, (err, data) => {
        if (err) return res.send("获取失败");
        res.send(data);
    });
    // return res.send("测试成功");
};

// 修改个人信息
exports.setUser = (req, res) => {
    console.log(req.body);
    let type = req.body.type;
    let newPrice = req.body.newPrice;
    let account = req.body.account;
    if (type == "account") {
        if (newPrice == account) {
            return res.send({
                s: 400,
                m: "请输入不同的手机号",
            });
        }
        let reg1 = /^\d{11}$/;
        if (!reg1.test(newPrice)) {
            return res.send({
                s: 400,
                m: "请输入11位数的号码",
            });
        }
        let sql1 = "select * from `backstage_user` where `account` = ?";
        db.query(sql1, newPrice, (err, data) => {
            if (err) {
                return res.send({
                    s: 400,
                    m: "修改失败",
                });
            }
            if (data.length > 0) {
                return res.send({
                    s: 400,
                    m: "修改失败,该号码已注册",
                });
            } else {
                let sql2 = `UPDATE backstage_user SET ${type} = ? WHERE account = ?`;
                db.query(sql2, [newPrice, account], (err, data) => {
                    if (err) {
                        return res.send({
                            s: 400,
                            m: "失败",
                        });
                    }
                    return res.send({
                        s: 200,
                        m: "修改成功",
                    });
                });
            }
        });
    } else {
        let sql2 = `UPDATE backstage_user SET ${type} = ? WHERE account = ?`;
        db.query(sql2, [newPrice, account], (err, data) => {
            if (err) {
                return res.send({
                    s: 400,
                    m: "失败",
                });
            }
            return res.send({
                s: 200,
                m: "修改成功",
            });
        });
    }
};

// 获取所有客户
exports.allUser = (req, res) => {
    let sql = "select * from `user`";
    db.query(sql, (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "获取失败",
            });
        }
        console.log(data);
        data.forEach((i) => {
            i.essay = JSON.parse(i.essay);
            i.likes = JSON.parse(i.likes);
            i.collect = JSON.parse(i.collect);
            i.essayNum = i.essay.length;
            i.likeNum = i.likes.length;
            i.collectNum = i.collect.length;
            if (i.s == 0) {
                i.sT = "违规用户";
            } else {
                i.sT = "正常账号";
            }
        });
        res.send({
            s: 200,
            m: "获取成功",
            data,
        });
    });
};

// 设置用户状态
exports.setUserS = (req, res) => {
    let id = req.body.id;
    let s = req.body.s;
    let sql = "UPDATE `user` SET `s` = ? WHERE id = ?";
    db.query(sql, [s, id], (err, data) => {
        if (err) {
            return res.send({
                s: 400,
                m: "修改失败",
            });
        }
        res.send({
            s: 200,
            m: "修改成功",
        });
    });
};


